package com.qianfeng.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.bean.JsonResult;
import com.qianfeng.pojo.Goods;
import com.qianfeng.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: GoodsController
 * @Description: TODO
 * @author: Buke
 * @date: 2021-09-22 15:26:04
 * @since JDK 1.8
 */
@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    private String uploadImage;

    //查询
    @RequestMapping("findGoodsInfoByPage")
    @ResponseBody
    public JsonResult findByPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "6") Integer pageSize) {

        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询所有用户
        List<Goods> goods = goodsService.findGoods();
        for (Goods good : goods) {
            System.err.println(good);
        }
        //封装到PageInfo对象中
        PageInfo<Goods> PageInfo = new PageInfo<Goods>(goods);
        //返回JsonResult对象
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setCount(PageInfo.getTotal());
        jsonResult.setData(PageInfo.getList());

        return jsonResult;
    }

    //删除多选用户
    @RequestMapping("deleteAll")
    @ResponseBody
    public String deleteAll(Integer[] ids) {
        goodsService.deleteAll(ids);
        return "success";
    }

    //删除单个用户
    @RequestMapping("deleteUserById")
    @ResponseBody
    public String deleteUserById(Integer id) {
        System.out.println(id);
        goodsService.deleteUserById(id);
        return "success";
    }

    //更新信息
    @RequestMapping("UpdateInformation")
    @ResponseBody
    public String UpdateInformation(Goods goods) {
        System.err.println("controller:" + goods);
        goodsService.UpdateInformation(goods);
        return "success";
    }

    //上传图片
    @RequestMapping("upload")
    @ResponseBody
    public JsonResult upload(MultipartFile file) {

        //上传的原文件名
        uploadImage = file.getOriginalFilename();

        uploadImage = UUID.randomUUID().toString().replace("-", "") + uploadImage;
        //声明上传的路径
        String uploadPath = "C:\\Users\\米\\Desktop\\GuimeiSQL\\1";
        try {
            file.transferTo(new File(uploadPath, uploadImage));
        } catch (IOException e) {
            e.printStackTrace();
        }
        JsonResult jsonResult = JsonResult.ok();

        jsonResult.setMsg("http://localhost:8083/guimei/" + uploadPath);

        return jsonResult;

    }

    //新增
    @RequestMapping("addGoodss")
    public String addGoodss(Goods goods) {
        //设置用户图片地址
        goods.setPicture("http://localhost:8083/guimei/" + uploadImage);

        System.out.println(goods);

        goodsService.addGoodss(goods);

        return "redirect:http://localhost:8080/index.html";
    }

}
